//处理easyui高度百分比
function fixHeight(percent){
	percent = percent/100;
    return (document.body.clientHeight) * percent ;      
}  
//处理easyui宽度百分比
function fixWidth(percent){
	percent = percent/100;
    return (document.body.clientWidth - 5) * percent ;      
}

//easyui-datagrid设置按钮是否可以编辑  type: 0初始化；1单选择； 2取消选择； 3全部选择
var selectNum;//记录总共选择的行数
function setDgBut(type,num){
	if(type == 1){//单选择
		selectNum = selectNum + num;
		if(selectNum == 1){
			setLinkButton('dg-edit','enable');//编辑
			setLinkButton('dg-error','enable');//禁用
			setLinkButton('dg-ok','enable');//启用
			setLinkButton('dg-delete','enable');//删除
			setLinkButton('dg-authorize','enable');//分配权限
		}else{
			setLinkButton('dg-edit','disable');//编辑
			setLinkButton('dg-error','disable');//禁用
			setLinkButton('dg-ok','disable');//启用
			setLinkButton('dg-delete','enable');//删除
			setLinkButton('dg-authorize','disable');//分配权限
		}
	}else if(type == 2){//取消
		selectNum = selectNum - num;
		if(selectNum == 1){
			setLinkButton('dg-edit','enable');//编辑
			setLinkButton('dg-error','enable');//禁用
			setLinkButton('dg-ok','enable');//启用
			setLinkButton('dg-delete','enable');//删除
			setLinkButton('dg-authorize','enable');//分配权限
		}else if(selectNum > 1){
			setLinkButton('dg-edit','disable');//编辑
			setLinkButton('dg-error','disable');//禁用
			setLinkButton('dg-ok','disable');//启用
			setLinkButton('dg-delete','enable');//删除
			setLinkButton('dg-authorize','disable');//分配权限
		}else if(selectNum < 1){
			setLinkButton('dg-edit','disable');//编辑
			setLinkButton('dg-error','disable');//禁用
			setLinkButton('dg-ok','disable');//启用
			setLinkButton('dg-delete','disable');//删除
			setLinkButton('dg-authorize','disable');//分配权限
		}
	}else if(type == 3){//全部选择
		selectNum = num;
		setLinkButton('dg-edit','disable');//编辑
		setLinkButton('dg-error','disable');//禁用
		setLinkButton('dg-ok','disable');//启用
		setLinkButton('dg-delete','enable');//删除
		setLinkButton('dg-authorize','disable');//分配权限
	}else if(type == 0){//初始化
		selectNum = num;//选中行数
		setLinkButton('dg-edit','disable');//编辑
		setLinkButton('dg-error','disable');//禁用
		setLinkButton('dg-ok','disable');//启用
		setLinkButton('dg-delete','disable');//删除
		setLinkButton('dg-authorize','disable');//分配权限
	}
}

//设置linkbutton状态
function setLinkButton(name,state){
	name = "[id^='"+name+"']";
	$(name).each(function(){
	    $(this).linkbutton(state);//分配权限
	});
}


function transMoney(s,n){
	var money = "";
	n = n > 0 && n <= 20 ? n : 2;
	s = s.toString();
	if(s != '0' && s.length > n){
		money = s.substring(0,(s.length-n))+"."+s.substring((s.length-n),s.length);
	}else{
		if(s == '0'){
			money = "0.00";
		}else{
			if(s.length <= n){
				var other = "";
				for(i=s.length;i<n;i++){
					other += "0";
				}
				money = "0."+other+s;
			}
		}
	}
	return money;
}

function checkDateBox(date){
	var a = /^(\d{4})-(\d{2})-(\d{2})$/;
	if(!a.test(date)){ 
		return false;
	}else{
		return true;
	} 
} 

//日期格式化 yyyy-MM-dd
function changeDateToString(now) {
	var dateTime = "未知";
	if(now != ""){
		var year = now.year + 1900;
	    var month =now.month + 1;
	    var day = now.date;
	    if (month < 10) {
	        month = "0" + month;
	    }
	    if (day < 10) {
	        day = "0" + day;
	    }
	    dateTime = year+"-"+month+"-"+day;
	}
    return dateTime;
}

//20161207jyx新增判断，用于车辆备案管理中的状态显示
function showCustomState(value) {
	var stateContent = "未获海关车辆备案号";
	if(value != "" && value != undefined){
		stateContent = "已获海关车辆备案号";
	}
    return stateContent;
}

//日期格式化 yyyy-MM-dd hh:mm:ss
function changeDateTimeToString(now) {
	var dateTime = "未知";
	if(now != ""&&now != undefined){
		var year = now.year + 1900;
	    var month =now.month + 1;
	    var day = now.date;
	    var hour = now.hours;
	    var minute =now.minutes;
	    var second =now.seconds;
	    if (month < 10) {
	        month = "0" + month;
	    }
	    if (day < 10) {
	        day = "0" + day;
	    }
	    if (hour < 10) {
	    	hour = "0" + hour;
	    }
	    if (minute < 10) {
	    	minute = "0" + minute;
	    }
	    if (second < 10) {
	    	second = "0" + second;
	    }
	    dateTime = year+"-"+month+"-"+day+" "+hour+":"+minute+":"+second;
	}
    
    return dateTime;
}


//表单数据转成json格式
function formToJson(formObj) {
	var o = {};
	var a = formObj.serializeArray();
	$.each(a, function() {
		if (this.value) {
			if (o[this.name]) {
				if (!o[this.name].push) {
					o[this.name] = [ o[this.name] ];
				}
				o[this.name].push(this.value || null);
			} else {
				if ($("[name='" + this.name + "']:checkbox", formObj).length) {
					o[this.name] = [ this.value ];
				} else {
					o[this.name] = this.value || null;
				}
			}
		}
	});
	return JSON.stringify(o);
}

//扩展easyui表单的验证
$.extend($.fn.validatebox.defaults.rules, {
    //验证汉字
    CHS: {
        validator: function (value) {
            return /^[\u0391-\uFFE5]+$/.test(value);
        },
        message: '只能输入汉字'
    },
    //验证数字
    number: {
        validator: function (value) {
            return /^[0-9]*$/.test(value) ;
        },
        message: '只能输入数字'
    },
    //最小字符数
    minLength: {     
        validator: function(value, param){
            return value.length >= param[0];     
        },     
        message: '请输入最小{0}位字符.'    
    },
    //最大字符数
    maxLength: {     
        validator: function(value, param){     
            return param[0] >= value.length;     
        },     
        message: '请输入最多{0}位字符'    
    },
    //字符数范围
    length: {     
        validator: function(value, param){     
            return value.length >= param[0] && param[1] >= value.length;     
        },     
        message: '请输入{0}-{1}位字符.'    
    },
    //验证英文
    english: {
        validator: function (value) {
            return /^[A-Za-z]+$/.test(value) ;
        },
        message: '只能输入英文字母'
    },
    //网址
    web: {
        validator: function(value){     
            return /^(http[s]{0,1}|ftp):\/\//i.test($.trim(value));     
        },     
        message: '网址格式错误'    
    },
    //电子邮箱
    email : {     
        validator: function(value){     
            return /^[a-zA-Z0-9_+.-]+\@([a-zA-Z0-9-]+\.)+[a-zA-Z0-9]{2,4}$/i.test($.trim(value));     
        },     
        message: '电子邮箱格式错误.'    
    },
    //移动手机号码验证
    mobile: {//value值为文本框中的值
        validator: function (value) {
            var reg = /^1[3|4|5|8|9]\d{9}$/;
            return reg.test(value);
        },
        message: '输入手机号码格式不准确'
    },
    //国内邮编验证
    zipcode: {
        validator: function (value) {
            var reg = /^[1-9]\d{5}$/;
            return reg.test(value);
        },
        message: '邮编必须是非0开始的6位数字'
    },
    //用户账号验证(只能包括 _ 数字 字母) 
    account: {//param的值为[]中值
        validator: function (value, param) {
            if (value.length < param[0] || value.length > param[1]) {
                $.fn.validatebox.defaults.rules.account.message = '用户名长度必须在' + param[0] + '至' + param[1] + '范围';
                return false;
            } else {
                if (!/^[\w]+$/.test(value)) {
                    $.fn.validatebox.defaults.rules.account.message = '用户名只能为数字、字母、下划线';
                    return false;
                } else {
                    return true;
                }
            }
        }, message: ''
    },
    //身份证
    docNum: {
    	validator: function (value) {
            var reg = /^(\d{6})(18|19|20)?(\d{2})([01]\d)([0123]\d)(\d{3})(\d|X)?$/;
            return reg.test(value);
        },
        message: '身份证格式不正确'
    },
    //开始时间小于结束时间
    md: { 
    	validator: function(value, param){ 
    	startTime2 = $(param[0]).datetimebox('getValue'); 
    	var d1 = $.fn.datebox.defaults.parser(startTime2); 
    	var d2 = $.fn.datebox.defaults.parser(value); 
    	varify=d2>d1; 
    	return varify; 

    	}, 
    	message: '结束时间要大于开始时间！' 
    }
    
})



//将表单数据转为json
function form2Json(id) {
    var arr = $("#" + id).serializeArray()
    var jsonStr = "";
    jsonStr += '{';
    for (var i = 0; i < arr.length; i++) {
        jsonStr += '"' + arr[i].name + '":"' + arr[i].value + '",'
    }
    jsonStr = jsonStr.substring(0, (jsonStr.length - 1));
    jsonStr += '}'

    var json = JSON.parse(jsonStr)
    return json
}
//日期格式化 yyyy-MM-dd
function changeDateToString(now) {
	var dateTime = "未知";
	if(now != "" && now != undefined){
		var year = now.year + 1900;
	    var month =now.month + 1;
	    var day = now.date;
	    if (month < 10) {
	        month = "0" + month;
	    }
	    if (day < 10) {
	        day = "0" + day;
	    }
	    dateTime = year+"-"+month+"-"+day;
	}
    return dateTime;
}

//数据字典
function  formatterDic(value,row,index,a,b,c){
	 var src=this.src;
	 var back="";
	 if(src!=null&&src!=undefined){
		 $.ajax({
	  			type : 'get',
	  			url : path+'/sys/dictionary/dicNamebykey.do',
	  			async:false,
	  			data : {
	  				'key' : src,
	  				'value' : value,
	  			},
	  			success : function(result) {
	  				if (result.httpCode == 'OK') {
	  					back= result.keyName;
	  				} else {
	  					back='';
	  				}
	  			}
	  		})
	 }
	return back;
}

//数据字典
function  departmentDic(value,row,index,a,b,c){
	 var src=this.src;
	 var back="";
	 if(src!=null&&src!=undefined){
		 $.ajax({
	  			type : 'get',
	  			url : path+'/business/department/departmentById.do',
	  			async:false,
	  			data : {
	  				'key' : src,
	  				'value' : value,
	  			},
	  			success : function(result) {
	  				if (result.httpCode == 'OK') {
	  					back= result.keyName;
	  				} else {
	  					back='';
	  				}
	  			}
	  		})
	 }
	return back;
}

//日期格式化
function  formatterDate(value,row,index,a,b,c){
	return changeDateToString(value);
}

/**
 * 将数值四舍五入(保留2位小数)后格式化成金额形式
 *
 * @param num 数值(Number或者String)
 * @return 金额格式的字符串,如'1,234,567.45'
 * @type String
 */
function formatCurrency(num) {
	if(num!=undefined&&num!=""){
    num = num.toString().replace(/\$|\,/g,'');
    if(isNaN(num))
    num = "0";
    sign = (num == (num = Math.abs(num)));
    num = Math.floor(num*100+0.50000000001);
    cents = num%100;
    num = Math.floor(num/100).toString();
    if(cents<10)
    cents = "0" + cents;
    for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
    num = num.substring(0,num.length-(4*i+3))+','+
    num.substring(num.length-(4*i+3));
    return (((sign)?'':'-') + num + '.' + cents);
	}else{
		return 0;
	}
}

//用于转化放行状态
function  formatStateImage(state){
	var back;
	if(state=='101402'){
		back="<img class='easyui-tooltip' src='"+path+"/images/jval/right.png'  title='已放行'/>";
	}else if(state=='101403'){
		back="<img class='easyui-tooltip' src='"+path+"/images/jval/error.png'  title='退车'/>";
	}else if(state=='101401'){
		back="<img class='easyui-tooltip' src='"+path+"/images/jval/warn.png'  title='待放行'/>";
	}
	return back;
}

//
function  formatBlackImage(value,row,index){
	var back;
	if(row.BLACK_CAR_NO!=null&&row.BLACK_CAR_NO!=""&&row.BLACK_CAR_NO!=undefined){
		var BLACK_REMARK="此车为黑名单车辆："+row.BLACK_REMARK;
		 back="<a href='#' title='"+BLACK_REMARK+"' class='easyui-tooltip' style='text-decoration:none;color:inherit;'><span style='background:url(../../images/tabicons.png) no-repeat;width:18px; line-height:18px; display:inline-block;background-position:-200px -100px;'>&nbsp;</span>"+value+" </a>"
	}else{
		back="<span >&nbsp;</span>"+value;
	}
	return back;
}

//datetimebox初始化默认当前时间转化
function dtbInitDate(date) {
	var day = date.getDate() > 9 ? date.getDate() : "0" + date.getDate();
	var month = (date.getMonth() + 1) > 9 ? (date.getMonth() + 1) : "0"
	+ (date.getMonth() + 1);
	var hor = date.getHours();
	var min = date.getMinutes();
	var sec = date.getSeconds();
	return date.getFullYear() + '-' + month + '-' + day+" "+hor+":"+min+":"+sec;
};

//图标展示
function  formatterImage(value,row,index){
	 var back;
	 if(row.operatorStatus=='100101'){
		 back="<img src='"+path+"/images/jval/right.png'  alt='通过'/>";
	 }else if(row.operatorStatus=='100102'){
		 back="<img src='"+path+"/images/jval/warn.png'  alt='警告'/>";
	 }else if(row.operatorStatus=='100103'){
		 back="<img src='"+path+"/images/jval/warning.png'  alt='拒绝'/>";
	 }
	 return back;
}
//清空
function cleanDate(){
	$('#searchForm').form('clear');
}
function onLoadSuccess(){
	 $(".easyui-tooltip").tooltip({
        onShow: function () {
            $(this).tooltip('tip').css({
                borderColor: '#000'
            });
        }
    });
}